$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/users/listuser',
        datatype: "json",
        colModel: [
			{ label: '用户ID', name: 'id', index: 'id', width: 60, key: true },
			{ label: '账号', name: 'mobile', index: 'mobile', width: 100 },
			{ label: '昵称', name: 'nickName', index: 'nick_name', width: 100 },
			{ label: '性别', name: 'sex', index: 'sex', width: 50 , formatter: function(value, options, row){
					if(value === 1){
						return  '男';
					}else if(value === 2){
						return  '女';
					}else{
						return "未知";
					}
				}},
			{ label: '直属上级ID', name: 'path', index: 'path', width: 100 },
			{ label: '用户层级关系', name: 'route', index: 'route', width: 100 },
			{ label: '推荐人ID', name: 'path', index: 'path', width: 100 },
			{ label: '代理等级ID', name: 'userLevelId', index: 'user_level_id',hidden:true },
			{ label: '代理等级', name: 'levelName', index: 'level_name', width: 80 },
			{ label: '邀请码', name: 'inviteCode',  width:100 },
			{ label: '状态', name: 'status',  width:100 , formatter: function(value, options, row){
                    if(value === 1){
                        return  '启用';
                    }else if(value === 2){
                        return  '禁用';
                    }else{
                        return "未知";
                    }
                }},
			{ label: '允许提转', name: 'activeStatus',  width:100 , formatter: function(value, options, row){
					if(value === 1){
						return  '允许';
					}else if(value === 2){
						return  '不允许';
					}else{
						return "允许";
					}
				}},
			{ label: '是否允许挂卖', name: 'isCompanyAccount',  width:100 , formatter: function(value, options, row){
					if(value === 1){
						return  '不允许';
					}else if(value === 0){
						return  '允许';
					}else{
						return "允许";
					}
				}},
			{ label: '注册时间', name: 'createTime',  width: 120 },
			{ label: '更新时间', name: 'updateTime',  width: 120 }
        ],
		height: 'auto',
		shrinkToFit: false,
		autoScroll: true,
		viewrecords: true,
		rowNum: 10,
		rowList: [10, 30, 50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth: true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader: {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames: {
			page: "page",
			rows: "limit",
			order: "order"
		},
		gridComplete: function () {
			//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-y": "hidden"});
		}
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			userId: null,
			mobile: null,
			path: null,
			status:null
		},
		showList: true,
		title: null,
		user: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.user = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.user.id == null ? "sys/users/save" : "sys/users/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.user),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/users/delete?id=" + id,
                        contentType: "application/json",
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/users/info/"+id, function(r){
                vm.user = r.user;
            });
		},
		reset:function(){
			vm.q = {userId:null,mobile:null, path:null,status:null};
			$("#combo").val('');
			$('table td input').val('');
			vm.reload();
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			var params=	{'userId':vm.q.userId,'mobile':vm.q.mobile,'path':vm.q.path,'status':vm.q.status};
			$("#jqGrid").jqGrid('setGridParam',{
				postData:params,
				page:1
			}).trigger("reloadGrid");
		}
	}
});

function updateStatus(status) {
	var id = getSelectedRow();
	if(id == null){
		return ;
	}
	var url = "sys/users/update";
    vm.user.id = id;
	vm.user.status = status;
	var description = "启用";
    if(status == 2){
		description = "禁用";
	}else if(status == 3){
		description = "禁止提币转账";
	}else if(status == 4){
		description = "恢复提币转账";
	}else if(status == 5){
		description = "禁止其及其下级 C2C 交易";
	}else if(status == 6){
		description = "禁止 C2C 交易";
	}else if(status == 0){
		description = "重置密码为: 123456 ";
	}

	confirm('确定要继续'+description+'操作？', function(){
		$.ajax({
			type: "POST",
			url: baseURL + url,
			contentType: "application/json",
			data: JSON.stringify(vm.user),
			success: function (r) {
				if (r.code === 0) {
					alert('操作成功', function () {
						vm.reload();
					});
				} else {
					alert(r.msg);
				}
			}
		});
	});
}

function audit(rowId, status) {
	$("#btn_no").show();
	$("#btn_ok").show();
	$("#btn_remove").show();
	var rowData = jQuery("#jqGrid").jqGrid("getRowData", rowId);
	$("#failCauseDiv").hide();
	if (status === 2) {
		$("#btn_no").hide();
		$("#btn_ok").hide();
	}
	$("#hiddenId").val(rowData.id);
	$("#frontImgUrl").html(rowData.frontImgUrl);
	$("#backImgUrl").html(rowData.backImgUrl);
	if (status === 3) {
		$("#failCauseDiv").show();
		$("#failCause").html(rowData.auditRemark);
	}
	$('#myModal').modal();
}

function success() {
	var id=$("#hiddenId").val();
	auditResult(2,id,"");
}

function refuse() {
	$('#failCauseModal').modal();
}

function savefailCause() {
	var id=$("#hiddenId").val();
	var failCause=$("#failCause1").val();
	if (failCause == null || failCause === ''){
		alert("请输入拒绝理由");
		return;
	}
	auditResult(3,id,failCause);
}

function auditResult(status,id,failCause){
	url = baseURL + "sys/users/audit?id=" + id + "&status=" + status + "&failCause=" + failCause;
	$.ajax({
		type: "POST",
		url: url,
		contentType: "application/json",
		success: function(r){
			if(r.code === 0){
				alert('操作成功', function(){
					vm.reload();
				});
			}else{
				alert(r.msg);
			}
		}
	});
}

function showImg(str)
{
    var newwin=window.open();
    newwin.document.write("<img height='auto' width='80%' src="+str+" />")
}